package com.xiangxiangjd.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangxiangjd.bean.Answer;
import com.xiangxiangjd.controller.helper.$;
import com.xiangxiangjd.bean.Topic;
import com.xiangxiangjd.enums.TopicType;
import com.xiangxiangjd.mapper.AnswerMapper;
import com.xiangxiangjd.mapper.TopicMapper;
import com.xiangxiangjd.service.AnswerService;
import com.xiangxiangjd.service.TopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.bean.result.ResultPage;
import com.xiangxiangjd.controller.model.ApiResultModel;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 肖守凯
 * @since 2021-05-05
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {


    @Autowired
    private AnswerService answerService;


    /**
     * 添加Topic
     *
     * @param topic 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    @Transactional
    public ApiResultModel addTopic(Topic topic) {


        /**
         * 添加时进行重新排序
         *
         * 排序的方法为：
         *  查询 当前给个sort 给后面的题 都进行+1 并且保存答案
         */
//        List<Topic> topics = baseMapper.selectList(new LambdaQueryWrapper<Topic>()
//                .eq(Topic::getTopicSort, topic.getTopicSort())
//                .eq(Topic::getQuestionnaireId, topic.getQuestionnaireId())
//        );
//        if (topics.size() > 0) {
//            baseMapper.updateSorts(topic.getTopicSort());
//        }
//
//        //单选题进行校验
//        if (topic.getTopicType().equals(TopicType.DANXUAN)) {
//            //单选题占时没有需要 校验预留代码块
//
//
//        }else if(topic.getTopicType().equals(TopicType.DUOXUAN)){
//            //多选题校验
//
//
//        }



        if (this.save(topic)) {

            List<Answer> answerList = topic.getAnswerList();

            //给每个答案的问题赋值
            List<Answer> collect = answerList.stream().map(obj -> {
                obj.setTopicId(topic.getId());
                return obj;
            }).collect(Collectors.toList());

            //添加所有的题目
            answerService.saveBatch(collect);


            return $.success();
        } else {
            return $.error("添加失败");
        }
    }

    /**
     * 编辑跳转页面的回显数据Topic
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public Topic selectTopicByIdTopic(String id) {
        return this.getById(id);
    }


    /**
     * 编辑Topic
     *
     * @param topic 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editTopic(Topic topic) {
        if (this.updateById(topic)) {
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    /**
     * 删除一条Topic
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel delTopics(List ids) {

        int i = baseMapper.deleteBatchIds(ids);

        return $.success(i);

    }

    /**
     * 分页查询Topic
     *
     * @param page  页码
     * @param limit 查几条
     * @param topic 前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findTopicByPage(Integer page, Integer limit, Topic topic) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("topic", topic);
        List<Topic> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Topic> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public List<Topic> getTopicAllAnswer(String questionnaireId, String type, Integer sort) {



        return baseMapper.selectTopicAllAnswer(questionnaireId,type,sort);
    }

    @Override
    public List<Topic> beforeAllTopic(String questionnaireId, Integer sort) {
        return baseMapper.beforeAllTopic(questionnaireId,sort);
    }

    @Override
    public List<Topic> getQuestionnaireAllTopic(String questionnaireId) {
        return baseMapper.selectQuestionnaireAllTopic(questionnaireId);
    }
}
